define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/show_location_apply/index' + location.search,
                    // add_url: 'shop/show_location_apply/add',
                    // edit_url: 'shop/show_location_apply/edit',
                    // del_url: 'shop/show_location_apply/del',
                    // multi_url: 'shop/show_location_apply/multi',
                    import_url: 'shop/show_location_apply/import',
                    table: 'wanlshop_shop_show_location_apply',
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='user_id']", form)
                    .addClass("selectpage")
                    .data("source", "user/user/index")
                    .data("primaryKey", "id")
                    .data("field", "nickname")
                    .data("orderBy", "id desc");
                $("input[name='shop_id']", form)
                    .addClass("selectpage")
                    .data("source", "wanlshop/shop/index")
                    .data("primaryKey", "id")
                    .data("field", "shopname")
                    .data("orderBy", "id desc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                search: false,
                commonSearch: true,
                searchFormVisible: true,
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('User_id'), visible: false, operate: 'LIKE %...%'},
                        {field: 'user.nickname', title: __('User_id'), operate: false},
                        {field: 'shop_id', title: __('Shop_id'), visible: false, operate: 'LIKE %...%'},
                        {field: 'shop.shopname', title: __('Shop_id'), operate: false},
                        {field: 'show_id', title: __('Show_id')},
                        {field: 'certificate', title: __('Certificate'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'refuse', title: __('Refuse'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'agree',
                                    text: '审核通过',
                                    title: '通过',
                                    classname: 'btn btn-xs btn btn-success btn-view btn-dialog',
                                    icon: 'fa fa-check',
                                    url: function (res) {
                                        return 'shop/show_location_apply/agree/ids/' + res.id;
                                    },
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        if(row.status == 0){
                                            return true;
                                        }
                                    },
                                    refresh:true
                                },{
                                    name: 'refuse',
                                    text: '审核驳回',
                                    title: '审核驳回',
                                    classname: 'btn btn-xs btn btn-danger btn-view btn-dialog',
                                    icon: 'fa fa-close',
                                    url: function (res) {
                                        return 'shop/show_location_apply/refuse/ids/' + res.id;
                                    },
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        if(row.status == 0){
                                            return true;
                                        }
                                    },
                                    refresh:true

                                },
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        agree: function () {
            Controller.api.bindevent();
        },
        refuse: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
